package logic

import (
	"context"
	"mall_srv/models"

	"mall_srv/good"
	"mall_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品发布
func (l *GoodsAddLogic) GoodsAdd(in *good.GoodsAddRequest) (*good.GoodsAddResponse, error) {
	var err error
	var g models.Goods
	g.GoodsTitle = in.GoodsTitle
	g.GoodsImage = in.GoodsImage
	g.GoodsPrice = float64(in.GoodsPrice)
	g.GoodsStock = in.GoodsStock
	g.GoodStatus = in.GoodStatus
	g.Status = in.Status
	if err = g.Create(); err != nil {
		logx.Info("商品发布失败", err)
		return nil, err
	}
	return &good.GoodsAddResponse{
		GoodsId: int64(g.ID),
	}, nil
}
